package com.zr.controller;

import com.zr.entity.Dept;
import com.zr.entity.Emp;
import com.zr.mapper.DeptMapper;
import com.zr.mapper.EmpMapper;
import com.zr.utils.UploadUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：Zhang
 * @date ：Created in 2024/12/19 11:04
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @RequestMapping("/list")
    public String list(HttpServletRequest request){
        List<Emp> list =empMapper.selectAll();
        //控制层如何保存数据到网页
        request.setAttribute("list",list);
        return "emp/list";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public int delete(Integer id){
        int i = empMapper.deleteByPrimaryKey(id);
        return i;
    }
    @RequestMapping("/update")
    public String update(Integer id,HttpServletRequest request){
        Emp emp = empMapper.selectByPrimaryKey(id);
        request.setAttribute("emp",emp);
        List<Dept> depts = deptMapper.selectAll();
        System.out.println();
        request.setAttribute("depts",depts);
        System.out.println(depts);
        return "emp/update";
    }
    /*@RequestMapping("/confirmUpdate")
    public String confirmUpdate(MultipartFile myfile, Emp emp,HttpServletRequest request){
        if(myfile.getSize()!=0){
            String s = UploadUtils.uploadOos(myfile);
            emp.setImages(s);
        }else {
            emp.setImages(null);
        }
        empMapper.updateByPrimaryKeySelective(emp);
        return "redirect:/emp/list";
    }*/
    @RequestMapping("/confirmUpdate")
    @ResponseBody
    public int confirmUpdate(@RequestBody Emp emp){
        int i = empMapper.updateByPrimaryKeySelective(emp);
        return i;
    }
    @RequestMapping("/upload")
    public String upload(MultipartFile file, Emp emp,HttpServletRequest request){
        String path = UploadUtils.uploadOos(file);
        emp.setImages(path);
        empMapper.insert(emp);
        return "redirect:/emp/list";
    }
    @RequestMapping("/listJson")
    @ResponseBody
    public List<Emp> listJson(HttpServletRequest request){
        List<Emp> list =empMapper.selectAll();
        return list;
    }
    @RequestMapping("/insert")
    @ResponseBody
    public int insert(@RequestBody Emp emp){
        int i = empMapper.insert(emp);
        return i;
    }
}
